<?
error_reporting(E_ALL ^ E_NOTICE);
include("common/auth.php");
include("common/errBack.php");
include("common/err_code.php");
include("common/func.inc.php");
include("class/ProductInfo.php");
include("class/Member.php");
include("class/DisOrder.php");

include("class/Sale.class.php");

$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,
    array("location" => "http://localhost/open/openService/pft_insideMX.php",
        "uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];
// if($memberID==1433){print_r($_REQUEST);exit;}
if(!$pid=abs($_REQUEST['pid'])){ getOut("index.html");}
$aid=abs($_REQUEST['aid']);
$pro=new ProductInfo($s,$pid,$aid);
$m=new Member($s,$memberID);
$disOrder=new DisOrder($s,$pro,$m);
if(p_match($begintime=$_REQUEST['begintime']))  exit("游玩时间含有非法字符");
if(p_match($ordername=$_REQUEST['ordername']))  exit("取票人含有非法字符");
if(p_match($ordertel=$_REQUEST['ordertel']) || !isphone($_REQUEST['ordertel']))  exit("取票人电话含有非法字符");
if(!$tnum=abs($_REQUEST['tnum']))  exit("门票数不能为零");

if(abs($_REQUEST['tnum']) >= 1000)  exit("门票数量必须在1000以内");

if($remoteorder=trim($_REQUEST['remoteorder'])){
	if(p_match($remoteorder)||strlen($remoteorder)>256) exit("远端订单号非法");
}
$paymode=abs($_REQUEST['paymode']);
$totalTnum=$tnum;
foreach((array)$_REQUEST['c_pids'] as $c_pid=>$c_num){
	if ($c_num >= 1000) {
		exit("门票数量必须在1000以内");
	}
	$totalTnum+=$c_num;
}

//if ($_REQUEST['c_pids']) print_r($_REQUEST);
$tourists=array();
$idcards=array();
for($n=1;$n<($totalTnum+1);$n++){

	if($_REQUEST['tourist_name_'.$n] && $_REQUEST['tourist_cert_'.$n]){
		$tourists[]=mysql_real_escape_string($_REQUEST['tourist_name_'.$n]);
		$idcards[]=mysql_real_escape_string($_REQUEST['tourist_cert_'.$n]);
	}
}
$option=array();
$option["pid"]=$pid;
$option["begintime"] = $begintime;
$option["leavetime"]=$_REQUEST['leavetime'];
$option["ordername"] = trim($ordername);
$option["ordertel"]  = $ordertel;
$option["tnum"]      = $tnum;
$option["remoteorder"]=$remoteorder;
$option["c_pids"]=$_REQUEST['c_pids'];

$option["from"]=$_REQUEST['from'];
$option['memo']=mysql_real_escape_string($_REQUEST['memo']);
$option['origin']=$_REQUEST['origin'];
if(isset($_REQUEST['sfz'])){$option['sfz']=$_REQUEST['sfz'];}
$option['tourists']=$tourists;
$option['idcards']=$idcards;
$option['paymode']=$paymode;
if(count($idcards)){
    $option['sfz'] = $idcards[0];
}
try{
	
	$r=$disOrder->order($option, $aid);
	write_logs(json_encode($r));
	$url="orderSuc.html?ordernum=".$r['orderNum'] . '&ot=' . md5($r['orderType']) ;
	if($_SESSION['dtype']==6){
		$daction="预订订单:{$r['orderNum']}";
		$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
		$GLOBALS['le']->query($str);
	}
}catch(DisOrderException $e){
	getOut("plist.html",$e->getMessage());
}catch(InterFaceException $e){
	$url="orderFailure.html?msg=".$e->getMessage()."：".$SoapRequestChk[$e->getCode()]."(".$e->getCode().")";
}catch(MsgException $e){
	$url="orderFailure.html?msg=".$e->getMessage()."：".telcode($e->getCode())."(".$e->getCode().")";
}
if($_REQUEST['saveTo']==1){//常用联系人存储
	$m->addCinfo($ordername,$ordertel);
}
header("Location:$url");
function getOut($url,$msg=null){
	$js="<script>";
	if($msg){ $js.="alert('$msg');" ;}
	$js.="window.location='$url'</script>";
	exit($js);
}

function write_logs($txt,$file="/var/www/html/new/d/logs/orderInfo_logs.txt"){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}
?>